#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

class Solution {
public:
    vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
        //判断r,c是否有效。
        int i = nums.size();
        int j = nums[0].size();
        if(r*c!=i*j){
            return nums;
        }
        vector<int> sonResult(c,0);
        vector<vector<int>> result(r,sonResult);
        int t = 0;
        int k = 0;
        for(int ii = 0;ii<i;++ii){
            for(int jj = 0;jj<j;++jj){
                if(k<c){
                    result[t][k] = nums[ii][jj];
                }
                else{
                    ++t;
                    k=0;
                    result[t][k] = nums[ii][jj];
                }
                ++k;
            }
        }
        return result;

    }
};

int main(){
    Solution S;
    vector<vector<int>> nums = {{1,2},{3,4}};
    vector<vector<int>> result = S.matrixReshape(nums,2,4);
    return 0;
}